package CCRMClient.GUI;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.SystemColor;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTextArea;

import CCRMClient.ClientStart;

import java.util.LinkedList;

import CCRMClient.Controllers.CatalogManager;
import Common.SERVER_COMMANDS;
import Common.Entities.Product;
import Common.Entities.Type;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

import Common.Entities.STATUS;
import javax.swing.ListSelectionModel;
import org.eclipse.wb.swing.FocusTraversalOnArray;
import java.awt.Component;

/**
 * This Class implements the following UCs:
 * 1. Update product
 * 2. Relate product to types
 * 3. Enable / Disable product
 * @author Ori Ziv
 *
 */
public class UpdateProductScreen extends JPanel {
	private JLabel lblUpdateProduct;
	private JLabel lblProductID;
	private JTextField textProductID;
	private JButton btnSearch;
	private JLabel lblAssignedCatNTyp;
	private JScrollPane scpAssignedCatNTyp;
	private JTable tblAssignedCatNTyp;
	private JScrollPane scpAvailableCatNTyp;
	private JTable tblAvailableCatNTyp;
	private JLabel lblAvailableCatNTyp;
	private JLabel lblProductName;
	private JTextField textProductName;
	private JLabel lblProductDescription;
	private JTextArea textAreaProductDescription;
	private JLabel lblBasePrice;
	private JTextField textBasePrice;
	private JLabel lblProductStatus;
	private JLabel lblPictureTitle;
	private JTextField textPictureTitle;
	private JLabel lblPicture;
	private JButton btnRelateSelected;
	private JButton btnNewCategory;
	private JButton btnNewType;
	private JButton btnGoBack;
	private JButton btnUpdate;
	private JButton btnBrowse;
	private JFileChooser fchProductPictureChooser;
	private File picture;
	private Image productPicture;
	private Product product;
	private LinkedList<Type> diff_types;
	private JComboBox cmbProductStatus;
	private JButton btnUnrelateSelected;

	/**
	 * Create the panel.
	 */
	public UpdateProductScreen() {

		initialize();
	}
	public UpdateProductScreen(Product product, LinkedList<Type> diff_types, File picture) {
		this();
		this.product = product;
		this.diff_types = diff_types;
		this.picture = picture;
		loadDataToScreen();
	}
	
	private void loadDataToScreen() {
		// load product data
		textProductID.setText(Long.toString(product.getProductID()));
		textProductID.setEnabled(true);
		textProductID.setEditable(false);
		btnSearch.setEnabled(false);
		
		textProductName.setText(product.getProductName());
		textProductName.setEnabled(true);
		textProductName.setEditable(true);
		
		textAreaProductDescription.setText(product.getProductDescription());
		textAreaProductDescription.setEnabled(true);
		textAreaProductDescription.setEditable(true);
		
		textBasePrice.setText(Float.toString(product.getProductBasePrice()));
		textBasePrice.setEnabled(true);
		textBasePrice.setEditable(true);
		
		switch (product.getProductStatus()) {
		case ACTIVE:
			cmbProductStatus.setSelectedIndex(0);
			break;
		case NOT_ACTIVE:
			cmbProductStatus.setSelectedIndex(1);
			break;
		case SUSPENDED:
			cmbProductStatus.setSelectedIndex(2);
			break;
		}
		cmbProductStatus.setEnabled(true);
		cmbProductStatus.setEditable(true);
		btnUpdate.setEnabled(true);
		
		// load product picture
		if (product.getPicture().getPictureTitle() != null)
			textPictureTitle.setText(product.getPicture().getPictureTitle());
		textPictureTitle.setEnabled(true);
		textPictureTitle.setEditable(true);
		if (picture != null) {
			BufferedImage bufPic = null;
			try {
				bufPic = ImageIO.read(picture);				
				Image fitPic = bufPic.getScaledInstance(lblPicture.getWidth(), lblPicture.getHeight(), Image.SCALE_SMOOTH);
				productPicture = new ImageIcon (fitPic).getImage();
				lblPicture.setIcon(new ImageIcon(productPicture));
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Problem loading product picture", "Update Product", JOptionPane.ERROR_MESSAGE);
			}
		}
		btnBrowse.setEnabled(true);
		
		// load assigned categories and types
		if (!product.getProductCategoriesAndTypes().isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblAssignedCatNTyp.getModel();
			dtm.removeRow(0);
			Object[] tblRow = new Object[3];
			Type typ = null;
			for (int i = 0 ; i < product.getProductCategoriesAndTypes().size() ; i ++) {
				typ = product.getProductCategoriesAndTypes().get(i);
				tblRow[0] = typ.getItmID();
				tblRow[1] = typ.getCategoryName();
				tblRow[2] = typ.getItmName();
				dtm.addRow(tblRow);
			}
		}
		tblAssignedCatNTyp.setEnabled(true);
		btnUnrelateSelected.setEnabled(true);
		
		// load available categories and types
		if (!diff_types.isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblAvailableCatNTyp.getModel();
			dtm.removeRow(0);
			Object[] tblRow = new Object[3];
			Type typ = null;
			for (int i = 0 ; i < diff_types.size() ; i ++) {
				typ = diff_types.get(i);
				tblRow[0] = typ.getItmID();
				tblRow[1] = typ.getCategoryName();
				tblRow[2] = typ.getItmName();
				dtm.addRow(tblRow);
			}
		}
		tblAvailableCatNTyp.setEnabled(true);
		btnRelateSelected.setEnabled(true);
	}
	
	private void initialize() {
		setSize(970, 870);
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		setBackground(Color.WHITE);
		setLayout(null);
		add(getLblUpdateProduct());
		add(getLblProductID());
		add(getTextProductID());
		add(getBtnSearch());
		add(getLblAssignedCatNTyp());
		add(getScpAssignedCatNTyp());
		add(getScpAvailableCatNTyp());
		add(getLblAvailableCatNTyp());
		add(getLblProductName());
		add(getTextProductName());
		add(getLblProductDescription());
		add(getTextAreaProductDescription());
		add(getLblBasePrice());
		add(getTextBasePrice());
		add(getLblProductStatus());
		add(getLblPictureTitle());
		add(getTextPictureTitle());
		add(getLblPicture());
		add(getBtnRelateSelected());
		add(getBtnNewCategory());
		add(getBtnNewType());
		add(getBtnGoBack());
		add(getBtnUpdate());
		add(getBtnBrowse());
		add(getCmbProductStatus());
		add(getBtnUnrelateSelected());
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{getTextProductID(), getBtnSearch(), getBtnGoBack()}));
	}
	JLabel getLblUpdateProduct() {
		if (lblUpdateProduct == null) {
			lblUpdateProduct = new JLabel("Update Product");
			lblUpdateProduct.setHorizontalAlignment(SwingConstants.CENTER);
			lblUpdateProduct.setForeground(Color.RED);
			lblUpdateProduct.setFont(new Font("David", Font.BOLD, 26));
			lblUpdateProduct.setBackground(Color.WHITE);
			lblUpdateProduct.setBounds(0, 0, 970, 49);
		}
		return lblUpdateProduct;
	}
	JLabel getLblProductID() {
		if (lblProductID == null) {
			lblProductID = new JLabel("Product ID:");
			lblProductID.setFont(new Font("David", Font.BOLD, 20));
			lblProductID.setBounds(10, 78, 164, 29);
		}
		return lblProductID;
	}
	JTextField getTextProductID() {
		if (textProductID == null) {
			textProductID = new JTextField();
			textProductID.setText("Fill in product ID");
			textProductID.setColumns(10);
			textProductID.setBackground(SystemColor.menu);
			textProductID.setBounds(184, 83, 251, 20);
		}
		return textProductID;
	}
	JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("Search");
			btnSearch.addActionListener(new BtnSearchActionListener());
			btnSearch.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnSearch.setBounds(491, 64, 148, 56);
		}
		return btnSearch;
	}
	JLabel getLblAssignedCatNTyp() {
		if (lblAssignedCatNTyp == null) {
			lblAssignedCatNTyp = new JLabel("Assigned Categories and Types:");
			lblAssignedCatNTyp.setFont(new Font("David", Font.BOLD, 20));
			lblAssignedCatNTyp.setBounds(10, 117, 299, 29);
		}
		return lblAssignedCatNTyp;
	}
	JScrollPane getScpAssignedCatNTyp() {
		if (scpAssignedCatNTyp == null) {
			scpAssignedCatNTyp = new JScrollPane();
			scpAssignedCatNTyp.setBounds(10, 153, 412, 187);
			scpAssignedCatNTyp.setViewportView(getTblAssignedCatNTyp());
		}
		return scpAssignedCatNTyp;
	}
	
	JTable getTblAssignedCatNTyp() {
		if (tblAssignedCatNTyp == null) {
			tblAssignedCatNTyp = new JTable();
			tblAssignedCatNTyp.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblAssignedCatNTyp.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null},
				},
				new String[] {
					"ID", "Category", "Type"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
			tblAssignedCatNTyp.setFont(new Font("David", Font.BOLD, 11));
		}
		return tblAssignedCatNTyp;
	}
	JScrollPane getScpAvailableCatNTyp() {
		if (scpAvailableCatNTyp == null) {
			scpAvailableCatNTyp = new JScrollPane();
			scpAvailableCatNTyp.setBounds(10, 460, 412, 187);
			scpAvailableCatNTyp.setViewportView(getTblAvailableCatNTyp());
		}
		return scpAvailableCatNTyp;
	}
	JTable getTblAvailableCatNTyp() {
		if (tblAvailableCatNTyp == null) {
			tblAvailableCatNTyp = new JTable();
			tblAvailableCatNTyp.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblAvailableCatNTyp.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null},
				},
				new String[] {
					"ID", "Category", "Type"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
				boolean[] columnEditables = new boolean[] {
					false, false, false
				};
				public boolean isCellEditable(int row, int column) {
					return columnEditables[column];
				}
			});
			tblAvailableCatNTyp.setFont(new Font("David", Font.BOLD, 11));
		}
		return tblAvailableCatNTyp;
	}
	JLabel getLblAvailableCatNTyp() {
		if (lblAvailableCatNTyp == null) {
			lblAvailableCatNTyp = new JLabel("Available Categories and Types:");
			lblAvailableCatNTyp.setFont(new Font("David", Font.BOLD, 20));
			lblAvailableCatNTyp.setBounds(10, 420, 299, 29);
		}
		return lblAvailableCatNTyp;
	}
	JLabel getLblProductName() {
		if (lblProductName == null) {
			lblProductName = new JLabel("Product Name:");
			lblProductName.setFont(new Font("David", Font.BOLD, 20));
			lblProductName.setBounds(447, 153, 164, 29);
		}
		return lblProductName;
	}
	JTextField getTextProductName() {
		if (textProductName == null) {
			textProductName = new JTextField();
			textProductName.setEnabled(false);
			textProductName.setEditable(false);
			textProductName.setColumns(10);
			textProductName.setBackground(SystemColor.menu);
			textProductName.setBounds(645, 158, 251, 20);
		}
		return textProductName;
	}
	JLabel getLblProductDescription() {
		if (lblProductDescription == null) {
			lblProductDescription = new JLabel("Product Description:");
			lblProductDescription.setFont(new Font("David", Font.BOLD, 20));
			lblProductDescription.setBounds(447, 193, 205, 29);
		}
		return lblProductDescription;
	}
	JTextArea getTextAreaProductDescription() {
		if (textAreaProductDescription == null) {
			textAreaProductDescription = new JTextArea();
			textAreaProductDescription.setLineWrap(true);
			textAreaProductDescription.setFont(new Font("Courier New", Font.PLAIN, 11));
			textAreaProductDescription.setEnabled(false);
			textAreaProductDescription.setEditable(false);
			textAreaProductDescription.setBorder(new LineBorder(new Color(0, 0, 0)));
			textAreaProductDescription.setBackground(SystemColor.menu);
			textAreaProductDescription.setBounds(447, 231, 449, 109);
		}
		return textAreaProductDescription;
	}
	JLabel getLblBasePrice() {
		if (lblBasePrice == null) {
			lblBasePrice = new JLabel("Product's Base Price:");
			lblBasePrice.setFont(new Font("David", Font.BOLD, 20));
			lblBasePrice.setBounds(447, 351, 205, 29);
		}
		return lblBasePrice;
	}
	JTextField getTextBasePrice() {
		if (textBasePrice == null) {
			textBasePrice = new JTextField();
			textBasePrice.setEnabled(false);
			textBasePrice.setEditable(false);
			textBasePrice.setColumns(10);
			textBasePrice.setBackground(SystemColor.menu);
			textBasePrice.setBounds(647, 351, 251, 20);
		}
		return textBasePrice;
	}
	JLabel getLblProductStatus() {
		if (lblProductStatus == null) {
			lblProductStatus = new JLabel("Product Status:");
			lblProductStatus.setFont(new Font("David", Font.BOLD, 20));
			lblProductStatus.setBounds(447, 382, 205, 29);
		}
		return lblProductStatus;
	}
	JLabel getLblPictureTitle() {
		if (lblPictureTitle == null) {
			lblPictureTitle = new JLabel("Picture's Title:");
			lblPictureTitle.setFont(new Font("David", Font.BOLD, 20));
			lblPictureTitle.setBounds(447, 415, 164, 29);
		}
		return lblPictureTitle;
	}
	JTextField getTextPictureTitle() {
		if (textPictureTitle == null) {
			textPictureTitle = new JTextField();
			textPictureTitle.setEnabled(false);
			textPictureTitle.setEditable(false);
			textPictureTitle.setColumns(10);
			textPictureTitle.setBackground(SystemColor.menu);
			textPictureTitle.setBounds(645, 420, 251, 20);
		}
		return textPictureTitle;
	}
	JLabel getLblPicture() {
		if (lblPicture == null) {
			lblPicture = new JLabel("");
			lblPicture.setBounds(596, 455, 300, 300);
		}
		return lblPicture;
	}
	JButton getBtnRelateSelected() {
		if (btnRelateSelected == null) {
			btnRelateSelected = new JButton("Relate Selected");
			btnRelateSelected.addActionListener(new BtnRelateSelectedActionListener());
			btnRelateSelected.setEnabled(false);
			btnRelateSelected.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnRelateSelected.setBounds(10, 658, 205, 56);
		}
		return btnRelateSelected;
	}
	JButton getBtnNewCategory() {
		if (btnNewCategory == null) {
			btnNewCategory = new JButton("New Category");
			btnNewCategory.addActionListener(new BtnNewCategoryActionListener());
			btnNewCategory.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnNewCategory.setBounds(161, 758, 127, 56);
		}
		return btnNewCategory;
	}
	JButton getBtnNewType() {
		if (btnNewType == null) {
			btnNewType = new JButton("New Type");
			btnNewType.addActionListener(new BtnNewTypeActionListener());
			btnNewType.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnNewType.setBounds(320, 758, 127, 56);
		}
		return btnNewType;
	}
	JButton getBtnGoBack() {
		if (btnGoBack == null) {
			btnGoBack = new JButton("Go Back");
			btnGoBack.addActionListener(new BtnGoBackActionListener());
			btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnGoBack.setBounds(10, 758, 127, 56);
		}
		return btnGoBack;
	}
	JButton getBtnUpdate() {
		if (btnUpdate == null) {
			btnUpdate = new JButton("Update");
			btnUpdate.addActionListener(new BtnUpdateActionListener());
			btnUpdate.setEnabled(false);
			btnUpdate.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnUpdate.setBounds(473, 758, 127, 56);
		}
		return btnUpdate;
	}
	JButton getBtnBrowse() {
		if (btnBrowse == null) {
			btnBrowse = new JButton("Browse");
			btnBrowse.addActionListener(new BtnBrowseActionListener());
			btnBrowse.setEnabled(false);
			btnBrowse.setToolTipText("Click here in order to update picture");
			btnBrowse.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnBrowse.setBounds(447, 455, 124, 23);
		}
		return btnBrowse;
	}
	JFileChooser getFchProductPictureChooser() {
		if (fchProductPictureChooser == null) {
			fchProductPictureChooser = new JFileChooser();
			FileNameExtensionFilter filter = new FileNameExtensionFilter("Image .JPG files", "jpg");
			fchProductPictureChooser.setFileFilter(filter);
		}
		return fchProductPictureChooser;
	}
	/**
	 * This inner class implements event listener for pressing Go Back button
	 * @author Ori Ziv
	 *
	 */
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			ClientStart.panelSwicher(new CatalogScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing New Category button
	 * @author Ori Ziv
	 *
	 */
	private class BtnNewCategoryActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new AddCategoryScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing New Type button
	 * @author Ori Ziv
	 *
	 */
	private class BtnNewTypeActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new AddTypeScreen());
		}
	}
	/**
	 * This inner class implements event listener for pressing Browse button
	 * @author Ori Ziv
	 *
	 */
	private class BtnBrowseActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int returnVal = getFchProductPictureChooser().showOpenDialog(UpdateProductScreen.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				picture = fchProductPictureChooser.getSelectedFile();
				BufferedImage bufPic = null;
				try {
					bufPic = ImageIO.read(picture);
					Image fitPic = bufPic.getScaledInstance(lblPicture.getWidth(), lblPicture.getHeight(), Image.SCALE_SMOOTH);
					productPicture = new ImageIcon (fitPic).getImage();
					lblPicture.setIcon(new ImageIcon(productPicture));
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null, "Problem loading product picture", "Add Product Picture", JOptionPane.ERROR_MESSAGE);
				}			
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Search button
	 * @author Ori Ziv
	 *
	 */
	private class BtnSearchActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			try {
				String search = textProductID.getText();
				try {
					Integer.parseInt(search);
				} catch (NumberFormatException e1) {
					JOptionPane.showMessageDialog(null, "Product ID must be a natural positive number", "Update Product", JOptionPane.INFORMATION_MESSAGE);
					textProductID.setText("Fill in product ID");
				}
				if (search.isEmpty() || search.equals("Fill in product ID")) throw new NullPointerException();
				try {
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.GET_PRODUCT_UPDATE, CatalogManager.searchProduct(SERVER_COMMANDS.GET_PRODUCT_UPDATE, search));
				} catch (IOException exp) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException exp) {
				JOptionPane.showMessageDialog(null, "Fill in product ID", "Update Product", JOptionPane.INFORMATION_MESSAGE);
				textProductID.setText("Fill in product ID");
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Search button
	 * @author Ori Ziv
	 *
	 */
	private class BtnUpdateActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				// make sure all mandatory fields are filled
				String productName = textProductName.getText();
				String productDescription = textAreaProductDescription.getText();
				STATUS productStatus = (STATUS) cmbProductStatus.getSelectedItem();
				String strBasePrice = textBasePrice.getText();
				String pictureTitle = textPictureTitle.getText();
				if (productName.isEmpty() || productDescription.isEmpty() || strBasePrice.isEmpty() || pictureTitle.isEmpty()) throw new NullPointerException();
				// prepare data to be sent to server
				
				product.setProductName(productName);
				product.setProductDescription(productDescription);
				product.setProductBasePrice(Float.parseFloat(strBasePrice));
				product.setProductStatus(productStatus);
				product.setPicture(CatalogManager.addProductPicture(picture.getAbsolutePath(), pictureTitle, Long.toString(product.getProductID())));
				try {
//					if (!productStatus.equals(product.getProductStatus()))
//						ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.UPDATE_PRODUCT_STATUS, CatalogManager.setProductStatus(productStatus, product.getProductID()));
					ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.UPDATE_PRODUCT, CatalogManager.updateProduct(product));
				} catch (IOException exp) {
					JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
					ClientStart.quitApp();
				}
			} catch (NullPointerException e) {
				JOptionPane.showMessageDialog(null, "Product Name\n"
													+ "Product Description\n"
													+ "Product Base Price\n"
													+ "and Picture title\n"
													+ "are mandatory fields.", "Update Product", JOptionPane.INFORMATION_MESSAGE);
			}
		}
	}
	/**
	 * This inner class implements event listener for pressing Unrelate Selected button
	 * @author Ori Ziv
	 *
	 */
	private class BtnUnrelateSelectedActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			int selected = tblAssignedCatNTyp.getSelectedRow();
			Object[] rowToUnrelate = new Object[3];
			DefaultTableModel dtm = (DefaultTableModel) tblAssignedCatNTyp.getModel();
			rowToUnrelate[0] = dtm.getValueAt(selected, 0);
			rowToUnrelate[1] = dtm.getValueAt(selected, 1);
			rowToUnrelate[2] = dtm.getValueAt(selected, 2);
			dtm.removeRow(selected);
			dtm = (DefaultTableModel) tblAvailableCatNTyp.getModel();
			dtm.addRow(rowToUnrelate);
			tblAssignedCatNTyp.repaint();
			tblAvailableCatNTyp.repaint();
			Type typ = new Type ((String)rowToUnrelate[0], (String) rowToUnrelate[2], null, (String) rowToUnrelate[1]);
			product.removeTypeFromProduct(typ);
		}
	}
	/**
	 * This inner class implements event listener for pressing Relate Selected button
	 * @author Ori Ziv
	 *
	 */
	private class BtnRelateSelectedActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int selected = tblAvailableCatNTyp.getSelectedRow();
			Object[] rowToRelate = new Object[3];
			DefaultTableModel dtm = (DefaultTableModel) tblAvailableCatNTyp.getModel();
			rowToRelate[0] = dtm.getValueAt(selected, 0);
			rowToRelate[1] = dtm.getValueAt(selected, 1);
			rowToRelate[2] = dtm.getValueAt(selected, 2);
			dtm.removeRow(selected);
			dtm = (DefaultTableModel) tblAssignedCatNTyp.getModel();
			dtm.addRow(rowToRelate);
			tblAssignedCatNTyp.repaint();
			tblAvailableCatNTyp.repaint();
			Type typ = new Type ((String)rowToRelate[0], (String) rowToRelate[2], null, (String) rowToRelate[1]);
			product.addTypeToProduct(typ);
		}
	}
	private JComboBox getCmbProductStatus() {
		if (cmbProductStatus == null) {
			cmbProductStatus = new JComboBox();
			cmbProductStatus.setEnabled(false);
			cmbProductStatus.setModel(new DefaultComboBoxModel(STATUS.values()));
			cmbProductStatus.setMaximumRowCount(3);
			cmbProductStatus.setBounds(645, 387, 251, 20);
		}
		return cmbProductStatus;
	}
	JButton getBtnUnrelateSelected() {
		if (btnUnrelateSelected == null) {
			btnUnrelateSelected = new JButton("Unrelate Selected");
			btnUnrelateSelected.addActionListener(new BtnUnrelateSelectedActionListener());
			btnUnrelateSelected.setFont(new Font("Tahoma", Font.BOLD, 12));
			btnUnrelateSelected.setEnabled(false);
			btnUnrelateSelected.setBounds(10, 351, 205, 56);
		}
		return btnUnrelateSelected;
	}
}
